import { CraftingSystem } from './CraftingSystem';
import { BlockType } from '@/blocks/types';
import { CraftingGrid } from './types';

/**
 * 合成系统测试工具
 * 用于验证《我的世界》合成配方是否正确实现
 */
export class CraftingTest {
  private craftingSystem: CraftingSystem;

  constructor() {
    this.craftingSystem = new CraftingSystem();
  }

  /**
   * 创建空的合成网格
   */
  private createEmptyGrid(): CraftingGrid {
    return [
      [{ type: null, count: 0 }, { type: null, count: 0 }, { type: null, count: 0 }],
      [{ type: null, count: 0 }, { type: null, count: 0 }, { type: null, count: 0 }],
      [{ type: null, count: 0 }, { type: null, count: 0 }, { type: null, count: 0 }]
    ];
  }

  /**
   * 在网格中放置物品
   */
  private placeItem(grid: CraftingGrid, row: number, col: number, type: BlockType, count: number = 1): void {
    if (row >= 0 && row < 3 && col >= 0 && col < 3) {
      grid[row][col] = { type, count };
    }
  }

  /**
   * 测试基础配方
   */
  public testBasicRecipes(): void {
    console.log('=== 测试基础合成配方 ===\n');

    // 测试1: 木板配方 (无形状)
    console.log('测试1: 原木 -> 木板');
    const grid1 = this.createEmptyGrid();
    this.placeItem(grid1, 0, 0, BlockType.LOG, 1);
    const result1 = this.craftingSystem.checkRecipe(grid1);
    console.log(`结果: ${result1 ? `可合成 ${result1.recipe.result.count} 个木板` : '无法合成'}\n`);

    // 测试2: 工作台配方 (有形状)
    console.log('测试2: 4个木板 -> 工作台');
    const grid2 = this.createEmptyGrid();
    this.placeItem(grid2, 0, 0, BlockType.PLANKS, 1);
    this.placeItem(grid2, 0, 1, BlockType.PLANKS, 1);
    this.placeItem(grid2, 1, 0, BlockType.PLANKS, 1);
    this.placeItem(grid2, 1, 1, BlockType.PLANKS, 1);
    const result2 = this.craftingSystem.checkRecipe(grid2);
    console.log(`结果: ${result2 ? `可合成 ${result2.recipe.result.count} 个工作台` : '无法合成'}\n`);

    // 测试3: 木棍配方
    console.log('测试3: 2个木板 -> 木棍');
    const grid3 = this.createEmptyGrid();
    this.placeItem(grid3, 0, 0, BlockType.PLANKS, 1);
    this.placeItem(grid3, 1, 0, BlockType.PLANKS, 1);
    const result3 = this.craftingSystem.checkRecipe(grid3);
    console.log(`结果: ${result3 ? `可合成 ${result3.recipe.result.count} 个木棍` : '无法合成'}\n`);

    // 测试4: 火把配方
    console.log('测试4: 煤炭 + 木棍 -> 火把');
    const grid4 = this.createEmptyGrid();
    this.placeItem(grid4, 0, 0, BlockType.COAL, 1);
    this.placeItem(grid4, 1, 0, BlockType.STICK, 1);
    const result4 = this.craftingSystem.checkRecipe(grid4);
    console.log(`结果: ${result4 ? `可合成 ${result4.recipe.result.count} 个火把` : '无法合成'}\n`);
  }

  /**
   * 测试工具配方
   */
  public testToolRecipes(): void {
    console.log('=== 测试工具合成配方 ===\n');

    // 测试木镐
    console.log('测试: 木镐配方');
    const grid1 = this.createEmptyGrid();
    this.placeItem(grid1, 0, 0, BlockType.PLANKS, 1);
    this.placeItem(grid1, 0, 1, BlockType.PLANKS, 1);
    this.placeItem(grid1, 0, 2, BlockType.PLANKS, 1);
    this.placeItem(grid1, 1, 1, BlockType.STICK, 1);
    this.placeItem(grid1, 2, 1, BlockType.STICK, 1);
    const result1 = this.craftingSystem.checkRecipe(grid1);
    console.log(`结果: ${result1 ? `可合成 ${result1.recipe.result.count} 个木镐` : '无法合成'}\n`);

    // 测试铁剑
    console.log('测试: 铁剑配方');
    const grid2 = this.createEmptyGrid();
    this.placeItem(grid2, 0, 1, BlockType.IRON_INGOT, 1);
    this.placeItem(grid2, 1, 1, BlockType.IRON_INGOT, 1);
    this.placeItem(grid2, 2, 1, BlockType.STICK, 1);
    const result2 = this.craftingSystem.checkRecipe(grid2);
    console.log(`结果: ${result2 ? `可合成 ${result2.recipe.result.count} 个铁剑` : '无法合成'}\n`);
  }

  /**
   * 测试建筑方块配方
   */
  public testBuildingRecipes(): void {
    console.log('=== 测试建筑方块合成配方 ===\n');

    // 测试木楼梯
    console.log('测试: 木楼梯配方');
    const grid1 = this.createEmptyGrid();
    this.placeItem(grid1, 0, 0, BlockType.PLANKS, 1);
    this.placeItem(grid1, 1, 0, BlockType.PLANKS, 1);
    this.placeItem(grid1, 1, 1, BlockType.PLANKS, 1);
    this.placeItem(grid1, 2, 0, BlockType.PLANKS, 1);
    this.placeItem(grid1, 2, 1, BlockType.PLANKS, 1);
    this.placeItem(grid1, 2, 2, BlockType.PLANKS, 1);
    const result1 = this.craftingSystem.checkRecipe(grid1);
    console.log(`结果: ${result1 ? `可合成 ${result1.recipe.result.count} 个木楼梯` : '无法合成'}\n`);

    // 测试箱子
    console.log('测试: 箱子配方');
    const grid2 = this.createEmptyGrid();
    this.placeItem(grid2, 0, 0, BlockType.PLANKS, 1);
    this.placeItem(grid2, 0, 1, BlockType.PLANKS, 1);
    this.placeItem(grid2, 0, 2, BlockType.PLANKS, 1);
    this.placeItem(grid2, 1, 0, BlockType.PLANKS, 1);
    this.placeItem(grid2, 1, 2, BlockType.PLANKS, 1);
    this.placeItem(grid2, 2, 0, BlockType.PLANKS, 1);
    this.placeItem(grid2, 2, 1, BlockType.PLANKS, 1);
    this.placeItem(grid2, 2, 2, BlockType.PLANKS, 1);
    const result2 = this.craftingSystem.checkRecipe(grid2);
    console.log(`结果: ${result2 ? `可合成 ${result2.recipe.result.count} 个箱子` : '无法合成'}\n`);
  }

  /**
   * 运行所有测试
   */
  public runAllTests(): void {
    console.log('🎮 《我的世界》合成系统测试开始 🎮\n');
    
    // 显示配方统计
    const stats = this.craftingSystem.getRecipeManager().getStats();
    console.log(`📊 配方统计: 总共 ${stats.total} 个配方，分为 ${stats.categories} 个分类\n`);
    
    this.testBasicRecipes();
    this.testToolRecipes();
    this.testBuildingRecipes();
    
    console.log('✅ 合成系统测试完成！');
  }

  /**
   * 显示所有配方（调试用）
   */
  public showAllRecipes(): void {
    this.craftingSystem.getRecipeManager().printAllRecipes();
  }

  /**
   * 搜索配方
   */
  public searchRecipes(query: string): void {
    console.log(`🔍 搜索配方: "${query}"`);
    const results = this.craftingSystem.getRecipeManager().searchRecipes(query);
    console.log(`找到 ${results.length} 个相关配方:\n`);
    
    results.forEach(recipe => {
      console.log(this.craftingSystem.getRecipeManager().formatRecipe(recipe));
      console.log('-'.repeat(50));
    });
  }
}

// 导出测试实例，可以在控制台中使用
export const craftingTest = new CraftingTest();